package com.example.asus.keshe;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by k42 on 2018-05-06.
 */

public class AddDemoActivity extends Activity implements View.OnClickListener,
        AdapterView.OnItemSelectedListener{

    private List<String> data = new ArrayList<String>();

    private Spinner spinner;

    private Button button;

    private ArrayAdapter<String> adapter;

    private String type;

    private MySqliteHelper mySqliteHelper;
    private EditText et_money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_demo);

        button = (Button)findViewById(R.id.button14);
        button.setOnClickListener(this);
        spinner = (Spinner)findViewById(R.id.spinner);
        et_money = (EditText) findViewById(R.id.et_money);

        data.add("工资");
        data.add("兼职");
        data.add("分红");
        data.add("其他");
        data.add("开销");

        //默认选中第一项
        type = data.get(0);

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,data);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(this);

        mySqliteHelper = new MySqliteHelper(this);
    }



    @Override
    public void onClick(View v) {

        Toast.makeText(this,"新增",Toast.LENGTH_SHORT).show();
        Toast.makeText(this,"被选中" + type,Toast.LENGTH_SHORT).show();

        SQLiteDatabase db = mySqliteHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("name",type);
        values.put("password",et_money.getText().toString());

        db.insert("person",null,values);
        db.close();


    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //Toast.makeText(this,"被选中" + data.get(position),Toast.LENGTH_SHORT).show();
        //每次选择的时候，把被选中的项赋值给type
        type = data.get(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
